﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpDrawStraightModelLineWithoutRefCmd.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "CmdStatusUtils.h"
#include "ActionDrawStraightElementInput.h"
#include "ActionDrawStraightElementCreator.h"
#include "GbmpSnapEndPointPriorityTypesPreprocessor.h"
#include "AddGbmpDynamicPannelUtils.h"
#include "GbmpControlIds.h"
#include "IUserTransaction.h"
#include "IModelView.h"
#include "IModelLine.h"
#include "ILine3d.h"
#include "IElementBasicInformation.h"
#include "IJournalCheckData.h"
#include "JournalUtils.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(GbmpDrawStraightModelLineWithoutRefCmd)

GbmpDrawStraightModelLineWithoutRefCmd::GbmpDrawStraightModelLineWithoutRefCmd()
    : GbmpCommandBase(ID_CMD_GBMP_DRAW_STRAIGHT_MODEL_LINE_WITHOUT_REF)
{
}

GbmpDrawStraightModelLineWithoutRefCmd::~GbmpDrawStraightModelLineWithoutRefCmd()
{
}

bool GbmpDrawStraightModelLineWithoutRefCmd::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

bool GbmpDrawStraightModelLineWithoutRefCmd::IsEnabled() const
{
    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}

OwnerPtr<IAction> GbmpDrawStraightModelLineWithoutRefCmd::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    ActionDrawStraightElementInput actionInput;
    // 定制Action
    actionInput.CommandId = GetCommandId();
    actionInput.DrawLineMode = DrawLineMode::Chained;
    actionInput.LineFinishedCallback = ActionDrawStraightElementInput::CreateOnLineFinishedCallback(&GbmpDrawStraightModelLineWithoutRefCmd::OnLineFinished, this);
    
    std::vector<Int64> priorityTypes;
    priorityTypes.emplace_back(typeid(IModelLine).hash_code());
    actionInput.SnapCandidatesPreprocessor = NEW_AS_OWNER_PTR(GbmpSnapEndPointPriorityTypesPreprocessor, priorityTypes);

    OwnerPtr<IAction> opAction = ActionDrawStraightElementCreator::Create(actionInput);

    // 开启动态上下文面板
    AddGbmpDynamicPannelUtils::SetupModelLineCreationContextWithoutRef(opAction.get(), ID_PAGE_GBMP_MODIFY);

    return TransferOwnership(opAction);
}

ElementId GbmpDrawStraightModelLineWithoutRefCmd::OnLineFinished(gcmp::IUiView* pCurrentView, const gcmp::Vector3d& startPt, const gcmp::Vector3d& endPt,
    const gcmp::ISnap* pStartSnap, const gcmp::ISnap* pEndSnap)
{
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView, ElementId::InvalidID, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_UNLESS(pModelView, ElementId::InvalidID, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制直线模型线"));
    DBG_WARN_AND_RETURN_UNLESS(opTransaction.get(), ElementId::InvalidID, L"opTransaction为空",L"GDMPLab",L"2024-03-30");

    // 将三维坐标点转化为平面上的二维点
    //ElementId planeId = pModelView->GetWorkPlaneId();
    //DBG_WARN_AND_RETURN_UNLESS(planeId.IsValid(), ElementId::InvalidID, L"planeId无效", L"GDMPLab", L"2023-12-20");

    OwnerPtr<ILine3d> opCurve = ILine3d::Create(startPt, endPt);
    IModelLine* pModelLine = IModelLine::Create(pDoc, TransferOwnership(opCurve), ElementCreationOptions::Normal);
    DBG_WARN_AND_RETURN_UNLESS(pModelLine, ElementId::InvalidID, L"创建ModelLine失败",L"GDMPLab",L"2024-03-30");

    // 记录检查点数据，用于日志回放比较
    OwnerPtr<IJournalCheckData> opXResultData = IJournalCheckData::Create();
    DBG_WARN_AND_RETURN_UNLESS(opXResultData, ElementId::InvalidID, L"opXResultData为空",L"GDMPLab",L"2024-03-30");
    opXResultData->SetName(L"X");
    opXResultData->SetData(startPt.X());
    JournalUtils::LogOrCompareCheckData(TransferOwnership(opXResultData));

    OwnerPtr<IJournalCheckData> opYResultData = IJournalCheckData::Create();
    DBG_WARN_AND_RETURN_UNLESS(opYResultData, ElementId::InvalidID, L"opYResultData为空",L"GDMPLab",L"2024-03-30");
    opYResultData->SetName(L"Y");
    opYResultData->SetData(startPt.Y());
    JournalUtils::LogOrCompareCheckData(TransferOwnership(opYResultData));

    OwnerPtr<IJournalCheckData> opZResultData = IJournalCheckData::Create();
    DBG_WARN_AND_RETURN_UNLESS(opZResultData, ElementId::InvalidID, L"opZResultData为空",L"GDMPLab",L"2024-03-30");
    opZResultData->SetName(L"Z");
    opZResultData->SetData(startPt.Z());
    JournalUtils::LogOrCompareCheckData(TransferOwnership(opZResultData));

    opTransaction->Commit();

    ElementId modelLineId = pModelLine->GetOwnerElement()->GetBasicInformation()->GetElementId();
    DBG_WARN_AND_RETURN_UNLESS(modelLineId.IsValid(), ElementId::InvalidID, L"modelLineId无效",L"GDMPLab",L"2024-03-30");

    return modelLineId;
}
